package WaitNotify;

import javax.sound.sampled.FloatControl;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2022-12-25
 * Time: 11:01
 */
public class ThreadDemo6 {
    private static final Object LOCK = new Object();
    private static boolean isCigarette = false;
    public static void main(String[] args) throws InterruptedException {
        new Thread(()->{
            synchronized (LOCK) {
                System.out.println("是否有烟" + isCigarette);
                if(!isCigarette) {
                    System.out.println("还没有烟,先歇会!!");
                    try {
                        LOCK.wait(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("是否有烟" + isCigarette);
                if(isCigarette) {
                    System.out.println("小南开始干活~~~");
                }
            }
        },"小南").start();

        for(int i =0;i<5;++i) {
            new Thread(()->{
                synchronized (LOCK) {
                    System.out.println("开始干活");
                }
            }).start();
        }

        Thread.sleep(1000);
        synchronized (LOCK) {
            isCigarette = true;
            LOCK.notify();
            System.out.println("烟送到了!!!");
        }
    }
}